<?php
class User extends AppModel {

    public $validate = array(
    'name' => array('rule'=>array('between',2,50),'allowEmpty'=>false,'message'=>'Between 2 and 50 characters'),
    'username' => array(
    'between'=>array('rule'=>array('between',3,50),'allowEmpty'=>false,'message'=>'Between 3 and 50 characters'),
    'is_unique'=>array('rule'=>array('check_unique','username'),'message'=>'Login already taken')
    ),
    'password' => array(
    'equal_passwords'=>array('rule' => 'equal_passwords','message' => 'Passwords dont equal'),
    'minlength'=>array('rule'=>array('minLength',5),'message'=>'Min length is 5 characters')
    ),
    'password_2' => array(
    'equal_passwords'=>array('rule' => 'equal_passwords','message' => 'Passwords dont equal'),
    'minlength'=>array('rule'=>array('minLength',5),'allowEmpty'=>false,'message'=>'Min length is 5 characters')),
    'email' => array('rule'=>'email','message'=>'Please insert a valid email'),
    'info_email' => 'numeric',
    );

    public $belongsTo = array(
    'Group' =>
    array('className' => 'Group',
    'foreignKey' => 'group_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'dependent' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    )
    );

    public $hasMany = array(
    'Article' =>
    array('className' => 'Article',
    'foreignKey' => 'user_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'dependent' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    ),

    'Comment' =>
    array('className' => 'Comment',
    'foreignKey' => 'user_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'dependent' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    ),

    'News' =>
    array('className' => 'News',
    'foreignKey' => 'user_id',
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'dependent' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    ),

    );

    /**
     * validation rule
     * @param $data - data to be valid
     * @return <bolean>
     */
    public function equal_passwords($data) {
        if ($this->data['User']['password_2'] === $this->data['User']['password']) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * validation rule
     * @param $data - data to be valid
     * @param $fieldName - name of the field
     * @return <boolean>
     */
    public function check_unique($data, $fieldName) {
        $valid = false;
        if(isset($fieldName) && $this->hasField($fieldName)) {
            $valid = $this->isUnique(array($fieldName => $data));
        }
        return $valid;
    }


    public function save_user($data) {
        if(!empty($data)) {
            $data['User']['password'] = Security::hash(Configure::read('Security.salt') . $data['User']['password']);
            $data['User']['password_2'] = Security::hash(Configure::read('Security.salt') . $data['User']['password_2']);
        }
        return $this->save($data);
    }

}
?>